package com.hspedu.homework;

import java.io.*;

/**
 * @Author Stringzhua
 * @Date 2024/3/24 16:40
 * description:
 * 使用BufferedReader读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上
 */
public class homework02 {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\qiyeke\\filecontroller\\story.txt";
        BufferedReader br = null;
        String line = "";
        int lineNum = 0;
        try {
            br = new BufferedReader(new FileReader(filePath));
            while ((line = br.readLine()) != null) {
                //未到文件末尾
                System.out.println(++lineNum + line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {//读到文件末尾直接关闭reader流
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
